package com.example.myapplication3;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class MyListActivity extends ListActivity implements Runnable{

    private static final String TAG ="MyListActivity" ;
    Handler handler;
    List<String> ret = new ArrayList<String>();
    @Override                 //实现列表
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_my_list);listactivity自带布局，不用布局,如果加了这个，不会用listavtivity式布局
        List<String> list1 = new ArrayList<String>();  //list列表对象
//        for (int i = 1; i < 100; i++) {
//            list1.add("item" + i);
//        }
        //String[] list_data={"one","two","three" ,"four"};
        ListAdapter adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, list1);//若为String[]那句，则将list1改为list_data
        setListAdapter(adapter);
        Thread thread=new Thread(this);
        thread.start();
        //接受从网页中传来的数据
        handler = new Handler() {
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 8) {
                    ArrayList<String> list = (ArrayList<String>) msg.obj;
                    ListAdapter adapter = new ArrayAdapter<String>(MyListActivity.this,
                            android.R.layout.simple_list_item_1, list);//表示该布局来源于android
                    setListAdapter(adapter);
                    Toast.makeText(MyListActivity.this, "ret size=" + list.size(), Toast.LENGTH_SHORT).show();
                }
                super.handleMessage(msg);
            }
        };
//        MyTask task=new MyTask();
//        task.setHandler(handler);
    }
    @Override
    public void run() {
        Log.i(TAG, "run:........");

        //下面这一段直接输入Thread.sleep(3000);，报错点击跳出catch
        try {
            Thread.sleep(3000); //加了延时并不是必要的
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //线程中完成的任务。。。。。
        // URL url = null;
        try {
//            Document doc= Jsoup.connect("https://www.usd-cny.com/bankofchina.htm").get();
//            Log.i(TAG,"run:title="+doc.title());
//            Element publicTime=doc.getElementsByClass("time").first();
//            Log.i(TAG,"run:time="+publicTime.html());
//            Element table=doc.getElementsByTag("table").first();
//            Elements trs=table.getElementsByTag("tr");   //Elements集合，Element单个
//            for(Element tr:trs){
//                Elements tds=tr.getElementsByTag("td");
//                if(tds.size()>0){
//                    //将获取数据传到列表中去
//                    String str=tds.first().text();
//                    Log.i(TAG,"run:td="+str);
//                    String val=tds.get(5).text();
//                    Log.i(TAG,"run:rate="+val);
//                    ret.add(str+"-->"+val);
//                }
//            }
            Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
            Elements tables = doc.getElementsByTag("table");
            Log.i(TAG, "run: "+doc.title());
            Element table6 = tables.get(1);
            Elements tds = table6.getElementsByTag("td");//Elements是一个集合
            for (int a = 0;a<tds.size();a+=8) {
                if (tds.size() > 0) {
                    String str = tds.get(a).text();
                    Log.i(TAG, "run:td:" + str);
                    String val = tds.get(a+5).text();
                    Log.i(TAG, "run: rate:" + val);
                    ret.add(str + "--->" + val);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回数据给主线程
        Message msg = handler.obtainMessage(8,ret);
        handler.sendMessage(msg);

    }
}